package com.uuah.server.transport;

import java.io.Serializable;
import java.util.Comparator;

/**
 * A PropertyScope is used to assoaciate a message property with a lifetime.
 * Some scopes may be very brief such as {@link #INVOCATION} scope which only
 * lasts until a service has been invoke or a longer running scope such as
 * {@link #SESSION}
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午03:15:54
 * @version 1.0.0
 */
public final class PropertyScope implements Serializable {
	private static final long serialVersionUID = -4792653762048974018L;

	public static final String INVOCATION_NAME = "invocation";
	public static final String REQUEST_NAME = "request";
	public static final String SESSION_NAME = "session";

	public static final PropertyScope INVOCATION = new PropertyScope(
			INVOCATION_NAME, 0);

	public static final PropertyScope REQUEST = new PropertyScope(REQUEST_NAME,
			1);

	public static final PropertyScope SESSION = new PropertyScope(SESSION_NAME,
			2);

	public static final PropertyScope[] ALL_SCOPES = new PropertyScope[] {
			INVOCATION, REQUEST, SESSION };

	private String scope;
	private int order;

	public PropertyScope(String scope, int order) {
		this.scope = scope;
		this.order = order;
	}

	public String getScope() {
		return scope;
	}

	public int getOrder() {
		return order;
	}

	public String toString() {
		return getScope();
	}

	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		PropertyScope that = (PropertyScope) o;

		if (order != that.order) {
			return false;
		}
		if (scope != null ? !scope.equals(that.scope) : that.scope != null) {
			return false;
		}

		return true;
	}

	public int hashCode() {
		int result;
		result = (scope != null ? scope.hashCode() : 0);
		result = 31 * result + order;
		return result;
	}

	public static class ScopeComparator implements Comparator, Serializable {
		private static final long serialVersionUID = -3346258000312580166L;

		public int compare(Object o, Object o1) {
			if (o == o1) {
				return 0;
			}
			if (o.equals(o1)) {
				return 0;
			}
			return (((PropertyScope) o).getOrder() < ((PropertyScope) o1)
					.getOrder() ? -1 : 1);
		}
	}
}
